package com.yiyuan.weixin.weixinInservice.Iml;

import com.yiyuan.weixin.Common.GlobalConstant;
import com.yiyuan.weixin.model.ReceiveXmlEntity;
import com.yiyuan.weixin.weixinInservice.Iml.AbsWXResponse;
import com.yiyuan.weixin.weixinInservice.format.*;

import javax.servlet.http.HttpServletResponse;

/**
 * Created by admin on 2017/3/28.
 *输入A或a 回复图文
 *输入B或b 回复图片
 *输入C或c 回复voice
 *输入D或d 回复vidoe
 *输入E或e 回复music
 *输入F或f 打开位置上报
 *输入G或g 关闭位置上报
 */
public class WXResposeCustomize extends AbsWXResponse {
    FormatXmlService formatXmlService;

    @Override
    public void wxRespond(HttpServletResponse resp, ReceiveXmlEntity xmlEntity) {
        String text=xmlEntity.getContent();
        if (actionTextFilter(resp,xmlEntity))
            return;

        switch (text)
        {
            case "A":
            case "a":
                formatXmlService=new FormatXmlNews();
                break;
            case "B":
            case "b":
                formatXmlService=new FormatXmlImage();
                break;
            case "C":
            case "c":
                formatXmlService=new FormatXmlVoice();
                break;
            case "D":
            case "d":
                formatXmlService=new FormatXmlVideo();
                break;
            case "E":
            case "e":
                formatXmlService=new FormatXmlMusic();
                break;
            case "F":
            case "f":
                GlobalConstant.LOCATION_CHENK=true;
                xmlEntity.setContent("上报位置已开启");
                formatXmlService=new FormatXmlText();
                break;
            case "G":
            case "g":
                GlobalConstant.LOCATION_CHENK=false;
                xmlEntity.setContent("上报位置已关闭");
                formatXmlService=new FormatXmlText();
                break;
            default:
                formatXmlService=new FormatXmlText();
        }
        text=formatXmlService.formatXml(xmlEntity);
        responseMessage(resp,text);
    }

    private   boolean  actionTextFilter(HttpServletResponse resp, ReceiveXmlEntity xmlEntity){
        String text=xmlEntity.getContent();
        StringBuffer stringBuffer=new StringBuffer();
        if (text.startsWith("GB-")){
            String strings[]=text.split("-");
            if (strings.length!=3)
                return false;

            // TODO: 2017/8/29 调用服务器；
            stringBuffer.append("您已成功购买");
            stringBuffer.append(strings[2]);
            stringBuffer.append("个");
            stringBuffer.append(strings[1]);
            xmlEntity.setContent(stringBuffer.toString());
            formatXmlService=new FormatXmlText();
            text=formatXmlService.formatXml(xmlEntity);
            responseMessage(resp,text);
            return true;
        }
        if (text.startsWith("GBO-")){
            String strings[]=text.split("-");
            if (strings.length!=4)
                return false;
            // TODO: 2017/8/29 调用服务器；
            stringBuffer.append("您已成功委托");
            stringBuffer.append(strings[2]);
            stringBuffer.append("个");
            stringBuffer.append(strings[1]);
            xmlEntity.setContent(stringBuffer.toString());
            formatXmlService=new FormatXmlText();
            text=formatXmlService.formatXml(xmlEntity);
            responseMessage(resp,text);
            return true;
        }
        if (text.startsWith("MB-")){
            String strings[]=text.split("-");
            if (strings.length!=3)
                return false;
            // TODO: 2017/8/29 调用服务器；
            stringBuffer.append("您已成功卖出");
            stringBuffer.append(strings[2]);
            stringBuffer.append("个");
            stringBuffer.append(strings[1]);
            xmlEntity.setContent(stringBuffer.toString());
            formatXmlService=new FormatXmlText();
            text=formatXmlService.formatXml(xmlEntity);
            responseMessage(resp,text);
            return true;
        }
        if (text.startsWith("MBO-")){
            String strings[]=text.split("-");
            if (strings.length!=4)
                return false;
            // TODO: 2017/8/29 调用服务器；
            stringBuffer.append("您已成功委托");
            stringBuffer.append(strings[2]);
            stringBuffer.append("个");
            stringBuffer.append(strings[1]);
            xmlEntity.setContent(stringBuffer.toString());
            formatXmlService=new FormatXmlText();
            text=formatXmlService.formatXml(xmlEntity);
            responseMessage(resp,text);
            return true;
        }
        return false;
    }
}
